/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#ifndef _INC_AK_VFS_
#define	_INC_AK_VFS_

#include "ak_types.h"
#include "ak_gears.h"
#include "ak_lock.h"

#ifdef __cplusplus
extern "C" {
#endif

#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>

typedef struct{
	int 	type;
	off_t 	size;
	char 	sPath[S_APP_MAX_PATH];
}_t_vfs_clue;


/* VFS Dir:
** Indexing Function, Limiting of Time & Size
** LRU & Tar Operation.
** Using AlphaSort to Sort the Files under the Dir.
*/

extern 	int 	SDir_Mk(const char * name);
extern	int 	SDir_Ls(const char * path, int flag, _t_vfs_clue * pClueAU, int  limit);



/* VFS Command:
** Tar
** Mv
** Rm
*/
#define SVFS_CMD_DIR_MK 	0X00
#define SVFS_CMD_DIR_RM		0X01
#define SVFS_CMD_DIR_MV		0X02
#define SVFS_CMD_DIR_TAR	0X03

extern	int 	SDir_CmdTar(const char * path, const char * name);
extern	int 	SDir_CmdRm(const char * path);
extern	int 	SDir_CmdMv(const char * src, const char * dst);


/* VFS TimeStamp */

typedef struct{
	/* Time Info */
	unsigned char 	t_year;			/* year : 1900 + */
	unsigned char	t_mon;			/* month */
	
	unsigned char	t_mday; 		/* day of the month */
	unsigned char	t_hour; 		/* hours */
	
	unsigned char	t_min;			/* minutes */
	unsigned char	t_sec;			/* seconds */

	unsigned short 	t_msec;			/* ms */
	unsigned short 	t_usec;			/* us */

	unsigned short  i_idx;			/* Index of the Item */
	unsigned int 	i_off;			/* Offset of the Item */
	unsigned int 	i_len;			/* Lengthof the Item */
	unsigned int 	i_reserv;		/* Reserved */
}_t_vfs_ti;

#define SVFS_TIMING(pTi, vTM, vMSec, vUSec) \
	do{	\
		(pTi)->t_year 	= (unsigned char)((vTM)->tm_year);\
		(pTi)->t_mon 	= (unsigned char)((vTM)->tm_mon);\
		(pTi)->t_mday 	= (unsigned char)((vTM)->tm_mday);\
		(pTi)->t_hour 	= (unsigned char)((vTM)->tm_hour);\
		(pTi)->t_min 	= (unsigned char)((vTM)->tm_min);\
		(pTi)->t_sec 	= (unsigned char)((vTM)->tm_sec);\
		(pTi)->t_msec	= (unsigned short)(vMSec);\
		(pTi)->t_usec	= (unsigned short)(vUSec);\
	}while(0)


static	__inline	int 	SVfs_Timing(_t_vfs_ti * pTi)
{
	struct tm	   *tm;
	struct timeval	tv;
	int 			ret;
	
	ret = -1;	
	if( pTi){
		/* Timing */
		gettimeofday(&tv, NULL);		tm = localtime(&(tv.tv_sec));
		SVFS_TIMING(pTi, tm, (tv.tv_usec/1000), (tv.tv_usec%1000));
		ret = 0;
	}		

	return ret;
}

/* VFS File Handle */
typedef struct{
	int 				iData;		/* Index of Data File */
	int 				iTotal;		/* Total of the Item*/
	int 				iOffset;	/* Current Offset */
	
	FILE 			 * 	hIndex;		/* Handle of Index File */
	FILE 			 * 	hData;		/* Handle of Data File */
	char 				wPath[S_APP_MAX_PATH]; 	/* Working Dir */
	char 				dPath[S_APP_MAX_PATH];	/* Current Dir */
}_t_vfs_hi;


extern	int 	SVfs_RecFile(_t_vfs_hi * pHI, _t_vfs_ti * pTi, void * pData, int lData);

/* VFS GC */
#define SVFS_MAX_GC 		10

typedef struct{
	_t_sysSemB 		 	wake;
	_t_sysCS 			lock;

	/* Simple Array Que to GC */
	int 				exFlag;
	
	int 				rdIx;
	int 				wrIx;
	char 				wPathA[SVFS_MAX_GC][S_APP_MAX_PATH];
}_t_vfs_gc;

extern	int 	SVfs_GcInit(_t_vfs_gc * pGc);
extern	int 	SVfs_GcTrigger(_t_vfs_gc * pGc, char * pPath, int lPath);
extern	int 	SVfs_GcSche(_t_vfs_gc * pGc);


#ifdef __cplusplus
}
#endif

#endif




